<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="Content-Style-Type" content="text/css" />
  <meta name="generator" content="pandoc" />
  <meta name="version" content="S5 1.1" />
  <meta name="author" content="" />
  <title>Best Practices for Writing Stata Code</title>
  <style type="text/css">
      code{white-space: pre-wrap;}
      span.smallcaps{font-variant: small-caps;}
      span.underline{text-decoration: underline;}
      div.column{display: inline-block; vertical-align: top; width: 50%;}
  </style>
  <!-- configuration parameters -->
  <meta name="defaultView" content="slideshow" />
  <meta name="controlVis" content="hidden" />
  <!-- style sheet links -->
  <link rel="stylesheet" href="www/slides.css" type="text/css" media="projection" id="slideProj" />
  <link rel="stylesheet" href="www/outline.css" type="text/css" media="screen" id="outlineStyle" />
  <link rel="stylesheet" href="www/print.css" type="text/css" media="print" id="slidePrint" />
  <link rel="stylesheet" href="www/opera.css" type="text/css" media="projection" id="operaFix" />
  <!-- S5 JS -->
  <script src="www/slides.js" type="text/javascript"></script>
</head>
<body>
<div class="layout">
<div id="controls"></div>
<div id="currentSlide"></div>
<div id="header"></div>
<div id="footer">
  <h1></h1>
  <h2>Best Practices for Writing Stata Code</h2>
</div>
</div>
<div class="presentation">
<div class="title-slide slide">
  <h1 class="title">Best Practices for Writing Stata Code</h1>
</div>
<div id="master-do-file" class="slide section level1">
<h1>Master do-file</h1>
<ul>
<li>As you might have noticed, we mentioned the creation of many code scripts in the previous slides</li>
<li>A big project can become very complex, and scripts need to be run in a certain order to create the right output</li>
<li>That could mean you’d need to write one extremely long script, or a different document with instructions about in which order to run all the scripts</li>
<li><span style="color:orange">However</span>, you can make a script that runs other scripts</li>
<li>This makes it easy for anyone reproducing your results to do it with ease</li>
</ul>
</div>
<div id="master-do-file-1" class="slide section level1">
<h1>Master do-file</h1>
<p>A master script is the main script that calls upon and runs all the other scripts of a project. It</p>
<ul>
<li>compactly and reproducibly runs all scripts needed for data work,</li>
<li>is the table of content for all data work,</li>
<li>allows for easy updates</li>
</ul>
</div>
<div id="master-do-file-2" class="slide section level1">
<h1>Master do-file</h1>
<p><img src="img/Master4.PNG" /></p>
</div>
<div id="master-do-file-3" class="slide section level1">
<h1>Master do-file</h1>
<ul>
<li>By reading the master script, someone external to the project should have a general understanding of what is being done at every step</li>
<li>If you want to see how a particular table or dataset or created, reading the master script should be enough to tell which script to look at</li>
<li>You can then select which parts of the code you want to run, or run all of it</li>
</ul>
</div>
<div id="do-file-header" class="slide section level1">
<h1>Do-file header</h1>
<p><span style="font-size:10%"></span></p>
<pre><code>/*******************************************************************************

    PROJECT NAME
    Task description
    
    Inputs:         input1.dta
                    input2.xlsx
    Outputs:        graph.png
                    table.xlsx
    Author:         Name [email]
    Last update:    Oct 2021
    Notes:            This is just a suggestion. 
                    Need to update it when new data is received

*******************************************************************************/</code></pre>
</div>
<div id="comments" class="slide section level1">
<h1>Comments</h1>
<p>Comments should be used to</p>
<ul>
<li>Describe <span style="color:orange">what</span> the code is doing
<ul>
<li>Organize the script</li>
<li>Simplify reading of complex code</li>
</ul></li>
<li>Explain <span style="color:orange">why</span> the code is doing things in a certain way
<ul>
<li>Anyone with a good understanding of Stata could eventually understand a command</li>
<li>But they should be spared from trying to understand your thought process</li>
</ul></li>
</ul>
</div>
<div id="loops" class="slide section level1">
<h1>Loops</h1>
<p><span style="color:orange"><strong>DON’T REPEAT YOURSELF</strong></span></p>
</div>
<div id="loops-1" class="slide section level1">
<h1>Loops</h1>
<p><span style="font-size:10%"></span></p>
<pre><code>gr pie process_value, over(procurement_type) legend(on position(4) cols(1)) 
gr pie process_value, over(bid_procedure)    legend(on position(4) cols(1)) 
gr pie process_value, over(process_type)     legend(on position(4) cols(1)) </code></pre>
</div>
<div id="loops-2" class="slide section level1">
<h1>Loops</h1>
<p><span style="color:orange"><strong>DON’T REPEAT YOURSELF</strong></span></p>
<ul>
<li>It can be difficult to find errors in repeated code</li>
<li>It is difficult to maintain repetitive code</li>
</ul>
</div>
<div id="loops-3" class="slide section level1">
<h1>Loops</h1>
<p>Numbers</p>
<pre class="{s}"><code>forvalues number = 1/5 {
    di `number&#39;
}</code></pre>
<p>General</p>
<pre class="{s}"><code>foreach word in &quot;hello&quot; &quot;goodbye&quot; {
    di &quot;`word&#39;!&quot;
}</code></pre>
</div>
<div id="loops-4" class="slide section level1">
<h1>Loops</h1>
<p>WET</p>
<pre><code>gr pie process_value, over(procurement_type) legend(on position(4) cols(1)) 
gr pie process_value, over(bid_procedure)    legend(on position(4) cols(1)) 
gr pie process_value, over(process_type)     legend(on position(4) cols(1)) </code></pre>
<p>DRY</p>
<pre class="{s}"><code>    foreach var of varlist procurement_type bid_procedure process_type {
        gr pie process_value, over(`var&#39;) plegend(on position(4) cols(1)) 
    }</code></pre>
</div>
<div id="resouces" class="slide section level1">
<h1>Resouces</h1>
<ul>
<li><a href="https://worldbank.github.io/stata-visual-library/">Stata Visual Library</a></li>
<li><a href="https://worldbank.github.io/dime-data-handbook/coding.html">Stata Style Guide</a></li>
<li><a href="https://osf.io/6fsz3/">Development Research in Practice Course</a></li>
<li><a href="https://data.princeton.edu/stata/markdown">Stata Markdown</a></li>
<li><a href="https://github.com/worldbank/dime-stata-training">Updates on this training</a></li>
</ul>
</div>
</div>
</body>
</html>
